package name.as3.cmdFrameworks.manager
{
import flash.display.DisplayObject;
import flash.interfaces.ISafeGlobalOwner;
import flash.utils.Dictionary;

import name.as3.cmdFrameworks.interfaces.IFacade;
import name.as3.cmdFrameworks.interfaces.addins.IMediator;
import name.as3.cmdFrameworks.interfaces.addins.IMediatorAddin;
import name.as3.cmdFrameworks.interfaces.IMediatorManager;

public class MediatorManager extends AbstractManager implements IMediatorManager
{
	private var m_mapMediator:Dictionary = new Dictionary();
	/**
	 *
	 */
	public function MediatorManager()
	{
	}
//==================================================================================================
// public
//==================================================================================================
	/**
	 * 添加中介
	 */
	public function addMediator(strType : String, 
								obMediator : IMediator, 
								obView		: DisplayObject = null,
								obAddins		: Vector.<IMediatorAddin> = null,
								strAddins	: Vector.< Vector.<String> > = null,
								dataAddins	: Vector.< Vector.<Object> > = null):void
	{
		if (null == facade.appContainer)
		{
			throw new Error("Must set appContainer! ");
		}
		if (strType != null && obMediator != null)
		{
			var m:IMediator = m_mapMediator[strType];
			if (null == m)
			{
				m_mapMediator[strType] = obMediator;
				obMediator.setFacade(this.facade, super.sg);
				if (obView != null)
				{
					obMediator.viewer = obView;
				}
				obView = obMediator.viewer;
				if (obView != null)
				{
					if (obView is ISafeGlobalOwner)
					{
						ISafeGlobalOwner(obView).sg = super.sg;
					}
					/*添加子级
					*/
					this.facade.appContainer.addChild(obView);
					/*
					应用插件
					*/
					if (obAddins != null)
					{
						for (var i:uint = 0; i < obAddins.length; i++)
						{
							var obAddin:IMediatorAddin = obAddins[0];
							obAddin.onAddMediator(this.facade.appContainer, obMediator);
						}
					}
				}
				/*End if(obView);
				*/
			}
			/*End if(null == m);
			*/
		}
		/*End if(strType != null);
		*/
	}
	/**
	 * 切换面板
	 */
	public function switchMediator(arrType : Array,
								arrHide : Array,
								strType : String, 
								obMediator : IMediator, 
								obView		: DisplayObject = null,
								obAddins		: Vector.<IMediatorAddin> = null,
								strAddins	: Vector.< Vector.<String> > = null,
								dataAddins	: Vector.< Vector.<Object> > = null):void
	{
		if (arrType)
		{
			for (var i:uint = 0; i < arrType.length; i++)
			{
				var blHide:Boolean;
				if (arrHide != null)
				{
					blHide = Boolean(arrHide[i]);
				}
				this.removeMediator(arrType[i], blHide);
			}
		}
		this.addMediator(strType, obMediator, obView, obAddins, strAddins, dataAddins);
	}
	/**
	 * 移除中介
	 */
	public function removeMediator(strType:String, blHide:Boolean=false):void
	{
		if (strType != null)
		{
			var m:IMediator = m_mapMediator[strType];
			if (m != null)
			{
				m_mapMediator[strType] = null;
				var obView:DisplayObject = m.viewer;
				if (obView != null)
				{
					/*移除面板并释放资源
					*/
					if (obView.parent != null)
					{
						/*移除面板
						*/
						obView.parent.removeChild(obView);
					}
				}
			}
		}
	}
	/**
	 * 获取中介
	 */
	public function getMediator(strType:String):IMediator
	{
		return m_mapMediator[strType];
	}
}
}